package com.daydayup.learn.serial;


import com.rocksdb.Example;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable, Externalizable {

    private static final long serialVersionUID = -8454942347880130272L;

    private String name;

    private int age;

    private String address;

    public static void main(String[] args) {
        String path = "/Users/shenxiang/Downloads/xxx";
        // 序列化
        xuliehua(path);
        // 反序列化
        fanxuliehua(path);
    }

    public static void fanxuliehua(String path) {
        try (
                FileInputStream fis = new FileInputStream(path);
                ObjectInputStream ois = new ObjectInputStream(fis);
        ){
            Object o = ois.readObject();
            System.out.println(o);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void xuliehua(String path) {
        Student s = new Student();
        s.setName("zhangsan");
        s.setAddress("aaaaaa");
        s.setAge(22);
        try (
                FileOutputStream fos = new FileOutputStream(path);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ){
            oos.writeObject(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {

    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

    }
}
